﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Security;

namespace Department.admin.Membership
{
    public partial class AddUser : System.Web.UI.Page
    {
        bool wasSortedDesc = AdminVariablesSingleton.WasSortedDesc;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.IsAuthenticated)
            {
                Response.Redirect("/admin/Login.aspx");
            }
            this.Page.Title = "Добави потребител";

            AdminVariablesSingleton.changeCSS(Master, "admin");
            if (!IsPostBack)
            {
                DropDownListType.DataSource = Roles.GetAllRoles();
                DropDownListType.DataBind();
                
            }
        }

        
        protected void sortByHeading_Click(object sender, CommandEventArgs e)
        {

        }

       


        protected void itemsView_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            

           
        }

        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            string userName = null;
            string password = null;
            string email = "";
            userName = TextBoxUserName.Text;
            password = TextBoxPassword.Text;
            try
            {
                email = TextBoxEmail.Text;
            }
            catch (FormatException exc)
            {
                exc.ToString();
                email = "";
            }
            try
            {
                System.Web.Security.Membership.CreateUser(userName, password, email);
            }
            catch (MembershipCreateUserException xc)
            {
                xc.ToString();
            }
            try
            {
                Roles.AddUserToRole(userName, DropDownListType.SelectedValue.ToString());
            }
            catch (Exception ekc)
            {
                ekc.ToString();
            }
            Response.Redirect("ViewAllUsers.aspx");

        }

        protected void ButtonReset_Click(object sender, EventArgs e)
        {
            TextBoxUserName.Text = "";
            TextBoxPassword.Text = "";
            TextBoxEmail.Text = "";
        }

    }
}